package 动态代理;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JavaProxy {

    public static void main(String[] args) {

        Say say=(Say)new DynamicProxy().bind(new SayImp());
        say.say();

    }

}



 interface Say{
    public void say();
}

class SayImp implements  Say{
    @Override
    public  void say(){
        System.out.println("你好");
    }
}

class DynamicProxy implements InvocationHandler {
    private Object originalObj;

    public Object bind(Object originalObj) {
        this.originalObj = originalObj;
        return Proxy.newProxyInstance(originalObj.getClass().getClassLoader(), originalObj.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("--------------begin-------------");
        Object invoke = method.invoke(originalObj, args);
        System.out.println("--------------end-------------");
        return invoke;
    }
}